/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.mer.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.mer.entity.MerCustomer;
import com.kaaction.modules.mer.service.MerCustomerService;

/**
 * 客户管理Controller
 * @author huanghaiming
 * @version 2017-05-17
 */
@Controller
@RequestMapping(value = "${adminPath}/mer/merCustomer")
public class MerCustomerController extends BaseController {

	@Autowired
	private MerCustomerService merCustomerService;
	
	@ModelAttribute
	public MerCustomer get(@RequestParam(required=false) String id) {
		MerCustomer entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = merCustomerService.get(id);
		}
		if (entity == null){
			entity = new MerCustomer();
		}
		return entity;
	}
	
	@RequiresPermissions("mer:merCustomer:view")
	@RequestMapping(value = {"list", ""})
	public String list(MerCustomer merCustomer, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<MerCustomer> page = merCustomerService.findPage(new Page<MerCustomer>(request, response), merCustomer); 
		model.addAttribute("page", page);
		return "modules/mer/merCustomerList";
	}

	@RequiresPermissions("mer:merCustomer:view")
	@RequestMapping(value = "form")
	public String form(MerCustomer merCustomer, Model model) {
		model.addAttribute("merCustomer", merCustomer);
		return "modules/mer/merCustomerForm";
	}

	@RequiresPermissions("mer:merCustomer:edit")
	@RequestMapping(value = "save")
	public String save(MerCustomer merCustomer, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, merCustomer)){
			return form(merCustomer, model);
		}
		merCustomerService.save(merCustomer);
		addMessage(redirectAttributes, "保存客户管理成功");
		return "redirect:"+Global.getAdminPath()+"/mer/merCustomer/?repage";
	}
	
	@RequiresPermissions("mer:merCustomer:edit")
	@RequestMapping(value = "delete")
	public String delete(MerCustomer merCustomer, RedirectAttributes redirectAttributes) {
		merCustomerService.delete(merCustomer);
		addMessage(redirectAttributes, "删除客户管理成功");
		return "redirect:"+Global.getAdminPath()+"/mer/merCustomer/?repage";
	}

}